/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProcedureDialog.java
 *
 * Created on Apr 25, 2011, 5:09:14 PM
 */

package jgaf.procedure;

import java.awt.Dimension;
import java.awt.Label;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import jgaf.editor.NoSuchEditorException;
import jgaf.environment.Environment;

/**
 *
 * @author hanis
 */
public class ProcedureDialog extends javax.swing.JDialog {

    private ProcedureDescriptor descriptor;
    private List<InputParamaterChooserPanel> parameterPanels;

    public ProcedureDialog(java.awt.Frame parent, ProcedureDescriptor descriptor, boolean fromProcedure) throws NoSuchEditorException {
        super(parent, true);
        parameterPanels = new ArrayList<InputParamaterChooserPanel>();
        this.descriptor = descriptor;
        initComponents();
        initMyComponents(fromProcedure);
    }

    private void initMyComponents(boolean fromProcedure) throws NoSuchEditorException {
        setTitle(descriptor.getName());
        //  getContentPane().setLayout(new Borde        rLayout());

        //JTextArea descriptionArea = new JTextArea();
        descriptionArea.setText(descriptor.getDescription());
        //  getContentPane().add(descriptionArea, BorderLayout.NORTH);

        //  JPanel panel = new JPanel(new GridLayout(procedure.getInputCardinality(), 3));

        //    getContentPane().add(panel, BorderLayout.CENTER);
        if (!fromProcedure) {
            for (ProcedureRepresentation representation : descriptor.getInputRepresentations()) {
                inputRepresentationsPanel.add(new InputRepresentationChooserPanel(representation));
            }
        } else {
            inputRepresentationsPanel.add(new Label(descriptor.getInputRepresentations().get(0).getDescription()));

        }



        for (ProcedureParameter parameter : descriptor.getParameters()) {
            InputParamaterChooserPanel paramPanel = new InputParamaterChooserPanel(parameter);
            parameterPanels.add(paramPanel);
            inputParametersPanel.add(paramPanel);
        }

        if(descriptor.hasOutput()) {
            outputDescription.setText(descriptor.getOutputRepresentation().getDescription());
        } else {
            outputDescription.setText("");
        }
        pack();
        centerDialog();
        setVisible(true);
    }

    private void setAllParameters() {
        for (InputParamaterChooserPanel panel : parameterPanels) {
            panel.setParameter();
        }
    }



    private void centerDialog() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - this.getHeight()) / 2);
        this.setLocation(x, y);
    }




    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionArea = new javax.swing.JTextArea();
        inputRepresentationsPanel = new javax.swing.JPanel();
        buttonRun = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        inputParametersPanel = new javax.swing.JPanel();
        outputPanel = new javax.swing.JPanel();
        outputDescription = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        descriptionArea.setColumns(20);
        descriptionArea.setEditable(false);
        descriptionArea.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        descriptionArea.setLineWrap(true);
        descriptionArea.setRows(5);
        descriptionArea.setWrapStyleWord(true);
        jScrollPane1.setViewportView(descriptionArea);

        inputRepresentationsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Input representations"));
        inputRepresentationsPanel.setLayout(new javax.swing.BoxLayout(inputRepresentationsPanel, javax.swing.BoxLayout.Y_AXIS));

        buttonRun.setText("Run");
        buttonRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRunActionPerformed(evt);
            }
        });

        buttonCancel.setText("Cancel");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        inputParametersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Input parameters"));
        inputParametersPanel.setLayout(new javax.swing.BoxLayout(inputParametersPanel, javax.swing.BoxLayout.Y_AXIS));

        outputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Output"));
        outputPanel.setLayout(new javax.swing.BoxLayout(outputPanel, javax.swing.BoxLayout.Y_AXIS));

        outputDescription.setText("output text");
        outputPanel.add(outputDescription);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonRun)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonCancel)
                .addContainerGap(488, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(inputRepresentationsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(inputParametersPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(outputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inputRepresentationsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inputParametersPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonRun)
                    .addComponent(buttonCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRunActionPerformed
        try {
            handleRunningProcedure();
            dispose();
        } catch (WrongProcedureInputException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_buttonRunActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed


    private void handleRunningProcedure() throws WrongProcedureInputException {
        for (ProcedureRepresentation representation : descriptor.getInputRepresentations()) {
            if(representation.getRepresentation() == null) {
                throw new WrongProcedureInputException("All input representations has to be set.");
            }
        }
        setAllParameters();
        for (ProcedureParameter parameter : descriptor.getParameters()) {
            if(parameter.getText() == null || parameter.getText().equals("")) {
                throw new WrongProcedureInputException("All parameters has to be set.");
            }
        }
        Environment.getInstance().getProcedureHandler().createAndShowNewProcedure(descriptor);
    }

    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonRun;
    private javax.swing.JTextArea descriptionArea;
    private javax.swing.JPanel inputParametersPanel;
    private javax.swing.JPanel inputRepresentationsPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel outputDescription;
    private javax.swing.JPanel outputPanel;
    // End of variables declaration//GEN-END:variables

}
